package com.beilang.hotline.user.page;

import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.codelookup.model.CodeModel;
import com.beilang.base.core.page.BasePage;
import com.beilang.base.core.model.LabelValueBean;
import com.beilang.base.exception.CheckException;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.CodeConstant;
import com.beilang.base.util.DateUtil;
import com.beilang.base.util.StringUtil;
import com.beilang.base.property.AppDefaultProperty;
import com.beilang.hotline.user.manager.UserMgr;
import com.beilang.hotline.user.model.UserModel;
import com.beilang.hotline.user.to.SearchTO;
import com.beilang.hotline.dept.manager.DeptMgr;
import com.beilang.hotline.dept.model.DeptModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@SuppressWarnings("unchecked")
public class UserPage extends BasePage{
    
	private UserMgr userMgr;

	private DeptMgr deptMgr;    

    private CodeLookupMgr codeLookupMgr;
	
	public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
		this.codeLookupMgr = codeLookupMgr;
	}

	public void setUserMgr(UserMgr userMgr) {
		this.userMgr = userMgr;
	}

    public void setDeptMgr(DeptMgr deptMgr) {
        this.deptMgr = deptMgr;
    }

    public List getRoleList(){
		List list=new ArrayList();
		list.add(this.getEmptyCodeModel());
		list.addAll(getAddRoleList());
		return list;
	}

	public List getAddRoleList(){
		return codeLookupMgr.getCodeList(CodeConstant.CODE_USER_ROLE_LIST);
	}

    public List getDeptList(){
        List list=new ArrayList();
        DeptModel d=new DeptModel();
        d.setDeptId(0L);
        d.setDeptName("");
        list.add(d);
		list.addAll(getAddDeptList());
		return list;
    }

    public List getAddDeptList(){
         return deptMgr.getAllDept();
    }

    public List getHandlerList(){
         return codeLookupMgr.getCodeList(CodeConstant.HANDLER_IND_LIST);
    }

    public List getUpdateDeptList(){
         return this.getAddDeptList();
    }    

    public List getUpdateRoleList(){
		return getAddRoleList();
	}

    public List getAddCodeIndList(){
        return codeLookupMgr.getCodeListWithFilter(CodeConstant.IND_LIST, CodeConstant.CODE_FILTER, new ArrayList());
    }

    public List getUpdateCodeIndList(){
         return getAddCodeIndList();
    }

    public List getChannelList(){
		return codeLookupMgr.getCodeList(CodeConstant.CODE_CHANNEL_LIST);
	}

    public List getRecordModeList(){
		return codeLookupMgr.getCodeList(CodeConstant.CODE_RECORD_MODE_LIST);
	}    

    public List getAccessList(){
        List list= codeLookupMgr.getCodeList(CodeConstant.CODE_ACCESS_FUNCTION_LIST);
        if(list!=null&&list.size()>0){
            List l= new ArrayList();
            LabelValueBean data;
            for(Object obj:list){
                CodeModel code=(CodeModel)obj;
                data=new LabelValueBean();
                data.setValue(code.getCodeId());
                data.setLabel(code.getCodeDesc());
                data.setAssigned(false);
                l.add(data);
            }
            return l;
        }
        return new ArrayList();
    }

    public List getCompanyTypeList(){
        List list= codeLookupMgr.getCodeList(CodeConstant.CODE_COMPANY_TYPE_LIST);
        if(list!=null&&list.size()>0){
            List l= new ArrayList();
            LabelValueBean data;
            for(Object obj:list){
                CodeModel code=(CodeModel)obj;
                data=new LabelValueBean();
                data.setValue(code.getCodeId());
                data.setLabel(code.getCodeDesc());
                data.setAssigned(false);
                l.add(data);
            }
            return l;
        }
        return new ArrayList();
    }

    public List getSearchCompanyTypeList(){
        return getCompanyTypeList();
    }

    public List getAddCompanyTypeList(){
        return getCompanyTypeList();
    }
    public List getViewCompanyTypeList(){
        return getCompanyTypeList();
    }
    public List getUpdateCompanyTypeList(){
       return getCompanyTypeList(); 
    }
    public List getAddAccessList(){
		return getAccessList();
	}

    public List getViewAccessList(){
		return getAccessList();
	}

    public List getUpdateAccessList(){
		return getAccessList();
	}    

    public PaginationHelper getSearchList(PaginationHelper helper,SearchTO searchTO){
		helper=userMgr.getUserList(helper, searchTO);
		return helper;
	}	
	
	public PaginationHelper deleteUserList(PaginationHelper helper,SearchTO searchTO,List deleteList){
        for (Object obj : deleteList) {
            UserModel userModel = (UserModel) obj;
            userMgr.deleteUser(userModel.getUserId());
        }
		return this.getSearchList(helper, searchTO);
	}
	
	public UserModel addUser(UserModel userModel) throws CheckException{
        UserModel u=this.getViewUser(userModel.getUserId());
        if(u!=null&& (!StringUtil.isNullOrBlank(u.getUserId()))){
            throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_RECORD_ALREADY_EXIST));
        }
        if(userModel.getUserRole().equalsIgnoreCase("R")){
            List list=userMgr.getUserListByChannel(userModel.getChannelNo());
            if(list!=null&&list.size()>0){
                if("Y".equalsIgnoreCase(userModel.getPopupInd())){
                    throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_CHANNEL_ALREADY_EXIST));
                }
                for (Object obj : list) {
                    UserModel user = (UserModel) obj;
                    if(user.getPopupInd().equalsIgnoreCase("Y")){
                        throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_CHANNEL_ALREADY_EXIST));
                    }
                }
            }
        }
        UserModel user=this.getLoginUser();
		userModel.setCreateId(user.getUserId());
		userModel.setUpdateId(user.getUserId());
		Date date = DateUtil.getDate();
		userModel.setCreateTime(date);
		userModel.setUpdateTime(date);
		userModel=userMgr.createUser(userModel);
		return userModel;
	}
	
	public UserModel getViewUser (String userId){
		UserModel userModel=userMgr.getUser(userId);

        
        return userModel;
	}
	
	public UserModel getUpdateUser (String userId){
		return getViewUser(userId);
	}	
	
	public UserModel updateUser(UserModel userModel) throws CheckException{
        if(userModel.getUserRole().equalsIgnoreCase("R")){
            List list=userMgr.getUserListByChannel(userModel.getChannelNo());
            if(list!=null&&list.size()==1){
                UserModel u = (UserModel) list.get(0);
                if(!u.getUserId().equalsIgnoreCase(userModel.getUserId())){
                    if(userModel.getPopupInd().equalsIgnoreCase("Y")||u.getPopupInd().equalsIgnoreCase("Y")){
                        throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_CHANNEL_ALREADY_EXIST));
                    }
                }
            }
            if(list!=null&&list.size()>1){
                if(userModel.getPopupInd().equalsIgnoreCase("Y")){
                    throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_CHANNEL_ALREADY_EXIST));
                }
                for (Object obj : list) {
                    UserModel u = (UserModel) obj;
                    if (!u.getUserId().equalsIgnoreCase(userModel.getUserId())) {
                        if(u.getPopupInd().equalsIgnoreCase("Y")){
                            throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_CHANNEL_ALREADY_EXIST));
                        }
                    }
                }
            }
        }
        if(!userModel.getUserRole().equalsIgnoreCase("R")){
            userModel.setPopupInd("");
            userModel.setChannelNo("");
        }
        UserModel user=this.getLoginUser();
		userModel.setUpdateId(user.getUserId());
		userModel.setUpdateTime(DateUtil.getDate());
        userModel=userMgr.saveUser(userModel);
		return userModel;
	}    
}